<?php
class Show
{
	static $hdl400;
	static $hdl500;
	
	static $hdlnot;
	static $hdlerr;

	private static $bdata;
	
	static function fault($msg, $jump='', $timeout=0)
	{
		if(is_ajax()) {
			self::ajaxLeave(__FUNCTION__, $msg, $jump, $timeout);
		}
		elseif(self::$hdlerr)
		{
			import('~/'.self::$hdlerr[0].Core::$convent['t_suffix']);
			call_user_func(array(ucfirst(self::$hdlerr[0]).'Controller', self::$hdlerr[1]), $msg, $jump, $timeout);
		}
		else
		{
			if(is_a($msg, 'Exception')) {
				throw $msg;
			}
			else{
				throw new Exception($msg);
			}
		}
		die;
	}

	static function _note($type, $msg, $jump='', $timeout=0)
	{
		if(is_ajax()) {
			self::ajaxLeave($type, $msg, $jump, $timeout);
		}
		elseif(self::$hdlnot)
		{
			call_user_func(array(ucfirst(self::$hdlnot[0]).'Controller', self::$hdlnot[1]), $msg, $jump, $timeout);
		}
		else
		{
			echo "Notice: $msg <br/>\n";
			echo ($jump?'<a href="'.$jump.'">返回</a>':'<a href="javascript:history.back(-1)">返回</a>')
				, ' | <a href="'.WEB_ROOT.'/">首页</a>';
		}
		die;
	}

	static function note($msg, $jump='', $timeout=0){
		self::_note(__FUNCTION__, $msg, $jump, $timeout);
	}

	static function warn($msg, $jump='', $timeout=0) {
		self::_note(__FUNCTION__, $msg, $jump, $timeout);
	}
	
	static function go400($msg=null)
	{
		header("HTTP/1.0 404 Page not available");
		null===$msg ? $msg="Page not available, {$_REQUEST['a']} - <i>[error in 404]</i>" : null;

		if(is_ajax()) {
			self::ajaxLeave(__FUNCTION__, $msg);
		}
		elseif(self::$hdl400)
		{
			import('~/'.self::$hdl400[0].Core::$convent['t_suffix']);
			call_user_func(array(ucfirst(self::$hdl400[0]).'Controller', self::$hdl400[1]), $msg);
		}
		else
		{
			echo $msg;
		}
		die;
	}
	static function go500($msg='Internal server error')
	{
		header("HTTP/1.0 500 Internal server error");

		if(is_ajax()) {
			self::ajaxLeave(__FUNCTION__, $msg);
		}
		elseif(self::$hdl500)
		{
			import('~/'.self::$hdl500[0].Core::$convent['t_suffix']);
			call_user_func(array(ucfirst(self::$hdl500[0]).'Controller', self::$hdl500[1]), $msg);
		}
		else
		{
			echo "<h3>500 - $msg</h3>";
		}
		die;
	}
	

	static function set400Handler($controller, $action)
	{
		self::$hdl400	= array($controller, $action);
	}
	static function set500Handler($controller, $action)
	{
		self::$hdl500	= array($controller, $action);
	}
	static function setNoticeHandler($controller, $action)
	{
		self::$hdlnot	= array($controller, $action);
	}
	static function setErrorHandler($controller, $action)
	{
		self::$hdlerr	= array($controller, $action);
	}

	public static function ajaxLeave($level, $message, $jump='', $timeout=0)
	{
		$tmp = array(
			'level'		=>	$level,
			'message'	=>	$message,
		);
		if($jump) {
			$tmp['jump']	= $jump;
			$tmp['time']	= $timeout;
		}

		if(self::$bdata) {
			$tmp	=	array_merge(self::$bdata, $tmp);
		}
		
		header('Content-type: application/json; charset=utf-8');
		echo json_encode($tmp);
		die;
	}

	/**
	 * 回传参数给Ajax方式请求函数
	 */
	public static function ajaxBand($ary)
	{
		self::$bdata	=	self::$bdata ? array_merge(self::$bdata, $ary) : $ary;
	}
	

	//--旧版本写法支持-------------

	static function error($msg, $jump='', $timeout=0){
		self::fault($msg, $jump, $timeout);
	}
	static function warning($msg, $jump='', $timeout=0) {
		self::warn($msg, $jump, $timeout);
	}
	static function notice($msg, $jump='', $timeout=0){
		self::note($msg, $jump, $timeout);
	}

}

?>
